package com.ldw.chapter4.config;

import org.springframework.security.web.authentication.WebAuthenticationDetails;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 4.2.3
 * <p>Title: MyWebAuthenticationDetails</p>
 * <p>Description: </p>
 *
 * @author: dwLiu
 * @date: 2020/3/22 16:26
 */
public class MyWebAuthenticationDetails extends WebAuthenticationDetails {

    private boolean imageCodeIsRight;
    private String imageCode;
    private String saveImageCode;
    /**
     * 补充用户提交的验证码和session保存的验证码
     * Records the remote address and will also set the session Id if a session already
     * exists (it won't create one).
     *
     * @param request that the authentication request was received from
     */
    public MyWebAuthenticationDetails(HttpServletRequest request) {
        super(request);
        this.imageCode = request.getParameter("captcha");
        HttpSession session = request.getSession();
        this.saveImageCode = (String) session.getAttribute("captcha");
        if(!StringUtils.isEmpty(this.saveImageCode)){
            session.removeAttribute("captcha");
            if(!StringUtils.isEmpty(imageCode)&&imageCode.equals(saveImageCode)){
                this.imageCodeIsRight = true;
            }
        }
    }

    public boolean isImageCodeIsRight() {
        return imageCodeIsRight;
    }
}
